#if DEBUG_VERSION
if (!dafos) {
  throw new Error("Dafos namespace not found!");
}
#endif

/**
 * Constructor
 */
dafos.GadgetUI = function(parent) {
  LOG("[GadgetUI]");
  this.dom_parent = parent;
  this.loading_keys = {};
  this.render();
};

dafos.GadgetUI.prototype.render = function() {
  var message_attrs = {
    "className" : "dafos-message",
    "style" : {
      "display" : "none"
    }
  };
  
  var error_attrs = {
      "className" : "dafos-error",
      "style" : {
        "display" : "none"
      }
    };
  
  var notice_attrs = {
    "className" : "dafos-notice"
  };
  
  var loadbox_attrs = {
    "className" : "dafos-loadbox",
    "style" : {
      "display" : "none",
      "position" : "absolute",
      "right" : "4px",
      "top" : "4px",
      "backgroundColor" : "#dd0000",
      "padding" : "4px",
      "color" : "#ffffff"
    }
  };
  
  var loadicon_attrs = {
    "src" : "@server.deploy.base@/img/loading.png"
  };
  
  this.dom_message = w23.e("div", message_attrs);
  this.dom_parent.parentNode.insertBefore(this.dom_message, this.dom_parent);  
  
  this.dom_error = w23.e("div", error_attrs);
  this.dom_parent.parentNode.insertBefore(this.dom_error, this.dom_parent);
  
  var report_link = document.createElement("a");
  report_link.href = "http://code.google.com/p/opensocialdevapp/issues/entry?template=Issue%20report";
  report_link.target = "_blank";
  report_link.innerHTML = "Report an issue";
  
  var request_link = document.createElement("a");
  request_link.href = "http://code.google.com/p/opensocialdevapp/issues/entry?template=Feature%20request";
  request_link.target = "_blank";
  request_link.innerHTML = "Request a feature";
  
  this.dom_parent.appendChild(w23.e("div", notice_attrs, 
    this.getVersion(), " - ", report_link, " - ", request_link));
  
  var icon = w23.e("img", loadicon_attrs);
  this.dom_loadmessage = w23.e("span", null);
  this.dom_loadbox = w23.e("div", loadbox_attrs, icon, " Loading... ");
  this.dom_loadbox.appendChild(this.dom_loadmessage);
  
  this.dom_parent.appendChild(this.dom_loadbox);
  gadgets.window.adjustHeight();
};

dafos.GadgetUI.prototype.getVersion = function() {
  var str = "OpenSocial Dev App"
#if OPENSOCIAL_0_7
  str += " [0.7]";
#elif OPENSOCIAL_0_8
  str += " [0.8]";
#endif

#if TRACKING
  str += " [track]";
#endif

  return str;
};

dafos.GadgetUI.prototype.showLoading = function(key) {
  if (key != null) {
    this.loading_keys[key] = true;
  }
  var keys = [];
  for (var item in this.loading_keys) {
    if (this.loading_keys.hasOwnProperty(item)) {
      keys.push(item);
    }
  }  
  this.dom_loadmessage.innerHTML = keys.join(",");
  this.dom_loadbox.style.display = "block";
  gadgets.window.adjustHeight();
};

dafos.GadgetUI.prototype.hideLoading = function(key) {
  if (key != null) {
    delete this.loading_keys[key];
  }
  
  var keys = [];
  for (var item in this.loading_keys) {
    if (this.loading_keys.hasOwnProperty(item)) {
      keys.push(item);
    }
  }      
  if (keys.length == 0) {
    this.dom_loadbox.style.display = "none";
  } else {
    this.dom_loadmessage.innerHTML = keys.join(",");
  }
  gadgets.window.adjustHeight();
};

dafos.GadgetUI.prototype.setMessage = function(text) {
  if (text && text != "") {
    LOG("[setMessage]", "Setting message", text);
    window.clearTimeout(this.dom_message_timeout);
    var messagebox= this.dom_message;
#if MYSPACE
    messagebox.innerHTML = text;
#else
    messagebox.innerHTML = gadgets.util.escapeString(text);
#endif
    messagebox.style.display = "block";
    gadgets.window.adjustHeight();
    this.dom_message_timeout = window.setTimeout(function() {
      LOG("[setMessage]", "Clearing message");
      messagebox.innerHTML = "";
      messagebox.style.display = "none";
      gadgets.window.adjustHeight();
    }, 5000);
  }
};

dafos.GadgetUI.prototype.setError = function(text) {
  if (text && text != "") {
    LOG("[displayError]", "text:", text);
    window.clearTimeout(this.dom_error_timeout);
    var messagebox= this.dom_error;
#if MYSPACE
    messagebox.innerHTML = text;
#else
    messagebox.innerHTML = gadgets.util.escapeString(text);
#endif
    messagebox.style.display = "block";
    gadgets.window.adjustHeight();
    this.dom_error_timeout = window.setTimeout(function() {
      LOG("[displayError]", "Clearing text");
      messagebox.innerHTML = "";
      messagebox.style.display = "none";
      gadgets.window.adjustHeight();
    }, 10000);
  }
};

